package com.example.musicplayer.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.musicplayer.musicClass.MusicInfo;
import com.example.musicplayer.R;
import com.example.musicplayer.fragment.dialogFragment.MusicListDialogFragment;

import java.util.List;

public class MusicListDialogAdapter extends BaseAdapter {
    //存储音乐列表的数据
    private final List<MusicInfo> mMusicList;
    private final Context mContext;
    //记录正在播放的歌曲在列表里的位置
    private int mNowMusicPosition;
    //列表字体的默认颜色
    private int mDefaultColor;
    //得到对话框fragment的对象
    private final MusicListDialogFragment mMusicListDialogFragment;
    private AnimationDrawable mAnimationDrawable;

    public MusicListDialogAdapter(List<MusicInfo> list, Context context, MusicListDialogFragment dialogFragment) {
        this(list, context, 0, dialogFragment);
    }

    public MusicListDialogAdapter(List<MusicInfo> list, Context context, int nowPosition, MusicListDialogFragment dialogFragment) {
        mMusicList = list;
        mContext = context;
        mNowMusicPosition = nowPosition;
        mMusicListDialogFragment = dialogFragment;
        mDefaultColor = mContext.getResources().getColor(R.color.black);
    }

    //设置正在播放的音乐的位置
    public void setNowMusicPosition(int position) {
        mNowMusicPosition = position;
    }

    //获取当前音乐的索引
    public int getNowMusicPosition() {
        return mNowMusicPosition;
    }

    public void setDefaultColor(int defaultColor) {
        mDefaultColor = defaultColor;
    }

    public int getDefaultColor() {
        return mDefaultColor;
    }

    public void musicPlayingStateChanged(boolean isPlaying) {
        if (mAnimationDrawable != null) {
            if (isPlaying) {
                mAnimationDrawable.start();
            } else {
                mAnimationDrawable.stop();
            }
        }
    }

    //返回数据列表大小
    @Override
    public int getCount() {
        return mMusicList.size();
    }

    //得到数据列表某一项的对象
    @Override
    public Object getItem(int position) {
        return mMusicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.music_list_dialog_item, null);
            viewHolder.mInfo = convertView.findViewById(R.id.textview_music_list_dialog_item_info);
            viewHolder.mDelete = convertView.findViewById(R.id.linear_layout_music_list_dialog_item);
            viewHolder.mPlaying = convertView.findViewById(R.id.imageview_music_list_dialog_item_playing);
            viewHolder.mImagePlaying = convertView.findViewById(R.id.playing);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String info = mMusicList.get(position).getTitle() + " - " + mMusicList.get(position).getArtist();
        viewHolder.mInfo.setText(info);
        if (position == mNowMusicPosition) {
            viewHolder.mInfo.setTextColor(mContext.getResources().getColor(R.color.spring_green));
            viewHolder.mInfo.setTextSize(18);
            viewHolder.mPlaying.setVisibility(View.VISIBLE);
            mAnimationDrawable = (AnimationDrawable) viewHolder.mImagePlaying.getDrawable();
            if (mMusicListDialogFragment.isMusicPlaying()) {
                mAnimationDrawable.start();
            }
        } else {
            viewHolder.mInfo.setTextColor(mDefaultColor);
            viewHolder.mInfo.setTextSize(15);
            viewHolder.mPlaying.setVisibility(View.GONE);
            ((AnimationDrawable) viewHolder.mImagePlaying.getDrawable()).stop();
        }
        ((ImageView) convertView.findViewById(R.id.image_delete)).setColorFilter(mDefaultColor, PorterDuff.Mode.SRC_IN);

        viewHolder.mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMusicListDialogFragment.deleteOneMusic(position);
            }
        });
        return convertView;
    }

    //定义一个类来加载控件，来保存控件的实例化
    static class ViewHolder {
        TextView mInfo;
        View mPlaying;
        View mDelete;
        ImageView mImagePlaying;
    }
}